﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace GameHungTrung.Entities
{
    class GameManager
    {
        #region MY CODE
        public static Texture2D textureRo;
        public static Texture2D textureRoNho;
        public static Texture2D textureEgg;
        public static Texture2D textureShit;
        public static Texture2D textureGa;
        public static Texture2D textureVit;
        public static Texture2D textureNgan;
        public static Texture2D textureDay;
        public static SpriteFont gameFont;
        public static Texture2D textureStartGame0;
        public static Texture2D textureStartGame1;
        public static Texture2D textureGameOver0;
        public static Texture2D textureGameOver1;
        public static Texture2D textureCancel0;
        public static Texture2D textureCancel1;
        public static Texture2D texturePointer;
        public static Texture2D textureFarmBackGround;
        public static Texture2D textureHuongDan;

        public static int iSpeedEgg;

        private Random random;
        private GraphicsDeviceManager graphics;
        private ContentManager Content;

        private KeyboardState lastKeyState;
        private MouseState lastMouseState;

        private int _iCountEgg;
        private int _iCountEgg2;
        private int _iCountEgg3;

        public RoHungTrung _Ro;
        public List<GiaCam> _lstGiaCam;
        public Rope _Rope;

        private GiaCam giacam1;
        private GiaCam giacam2;
        private GiaCam giacam3;

        private GiaCam.Direction CameraDirection;
        private int iXScreen = 0;

        public GameManager(ContentManager c, GraphicsDeviceManager g)
        {
            iSpeedEgg = 2;
            lastKeyState = Keyboard.GetState();
            lastMouseState = Mouse.GetState();
            random = new Random();
            Content = c;
            LoadContent();
            graphics = g;

            _Ro = new RoHungTrung(textureRo, RoHungTrung.constLEFT, RoHungTrung.constTOP, textureRo.Width, textureRo.Height);
            _Rope = new Rope(textureDay, Rope.InitLeft, Rope.InitTop, textureDay.Width, textureDay.Height);
            InitListGiaCam();

            InitGiaCamReady();

            _iCountEgg = 0;
            _iCountEgg2 = 0;
            _iCountEgg3 = 0;
        }

        public void LoadContent()
        {
            textureFarmBackGround = this.Content.Load<Texture2D>(".\\images\\farm");

            textureRo = Content.Load<Texture2D>(".\\images\\Ro");
            textureRoNho = Content.Load<Texture2D>(".\\images\\ronho");
            textureEgg = Content.Load<Texture2D>(".\\images\\egg");
            textureShit = Content.Load<Texture2D>(".\\images\\shit");
            textureGa = Content.Load<Texture2D>(".\\images\\ga");
            textureVit = Content.Load<Texture2D>(".\\images\\vit");
            textureNgan = Content.Load<Texture2D>(".\\images\\ngan");
            textureDay = Content.Load<Texture2D>(".\\images\\day");
            gameFont = this.Content.Load<SpriteFont>(".\\fonts\\fontscores");

            textureStartGame0 = this.Content.Load<Texture2D>(".\\images\\start_new_game_0");
            textureStartGame1 = this.Content.Load<Texture2D>(".\\images\\start_new_game_1");
            textureGameOver0 = this.Content.Load<Texture2D>(".\\images\\gameover_main_menu_0");
            textureGameOver1 = this.Content.Load<Texture2D>(".\\images\\gameover_main_menu_1");
            texturePointer = this.Content.Load<Texture2D>(".\\images\\pointer");
            textureHuongDan = this.Content.Load<Texture2D>(".\\images\\tips");
            textureCancel0 = this.Content.Load<Texture2D>(".\\images\\cancel_0");
            textureCancel1 = this.Content.Load<Texture2D>(".\\images\\cancel_1");
        }

        public void UpdateTutorial()
        {
            if (isInBounds(Mouse.GetState().X, Mouse.GetState().Y, 300, 500, textureCancel1.Width, textureCancel1.Height)) //click cancel
            {
                if (lastMouseState.LeftButton == ButtonState.Pressed && Mouse.GetState().LeftButton == ButtonState.Released)
                {
                    Game1.State = Game1.GameState.Running;
                    for (int i = 0; i < _lstGiaCam.Count; i++)
                    {
                        _lstGiaCam[i].StartTimer();
                    }
                }
            }
            lastMouseState = Mouse.GetState();
        }

        public void DrawTutorial(SpriteBatch g)
        {
            g.DrawString(gameFont, "TUTORIAL", new Vector2(300, 200), Color.Red);
            if (isInBounds(Mouse.GetState().X, Mouse.GetState().Y, 300, 500, textureGameOver1.Width, textureGameOver1.Height))
            {
                g.Draw(textureCancel0, new Rectangle(300, 500, textureCancel0.Width, textureCancel0.Height), Color.White);
            }
            else
            {
                g.Draw(textureCancel1, new Rectangle(300, 500, textureCancel1.Width, textureCancel1.Height), Color.White);
            }
            g.Draw(textureHuongDan, new Rectangle(0, 250, textureHuongDan.Width, textureHuongDan.Height), Color.White);
            g.Draw(textureDay, new Rectangle(0, 170, 1024, 10), Color.White);
            g.Draw(texturePointer, new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, texturePointer.Width, texturePointer.Height), Color.White);
        }
        public void DrawGameOver(SpriteBatch g)
        {
            g.DrawString(gameFont, "GAME OVER", new Vector2(300, 250), Color.Red);
            if (isInBounds(Mouse.GetState().X, Mouse.GetState().Y, 300, 300, textureGameOver1.Width, textureGameOver1.Height))
            {
                g.Draw(textureGameOver0, new Rectangle(300, 300, textureGameOver0.Width, textureGameOver0.Height), Color.White);
            }
            else
            {
                g.Draw(textureGameOver1, new Rectangle(300, 300, textureGameOver1.Width, textureGameOver1.Height), Color.White);
            }
            g.Draw(textureDay, new Rectangle(0, 170, 1024, 10), Color.White);
            g.Draw(texturePointer, new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, texturePointer.Width, texturePointer.Height), Color.White);
        }
        public void UpdateGameReady()
        {
            CameraMove();
            giacam1.Move();
            giacam2.Move();
            giacam3.Move();
            if (isInBounds(Mouse.GetState().X, Mouse.GetState().Y, 350, 200, textureStartGame1.Width, textureStartGame1.Height))
            {
                if (lastMouseState.LeftButton == ButtonState.Pressed && Mouse.GetState().LeftButton == ButtonState.Released)
                {
                    Game1.State = Game1.GameState.Running;
                    if (Game1.isShowTutorial == false)
                    {
                        Game1.Screen = Game1.GameScreen.Tutorial;
                        for (int i = 0; i < _lstGiaCam.Count; i++)
                        {
                            _lstGiaCam[i].InitTimer();
                            _lstGiaCam[i].StopTimer();
                        }
                        Game1.isShowTutorial = true;
                        Game1.State = Game1.GameState.Pause;
                    }
                    else
                    {
                        for (int i = 0; i < _lstGiaCam.Count; i++)
                        {
                            _lstGiaCam[i].InitTimer();
                        }
                    }
                }
            }
           
            lastMouseState = Mouse.GetState();
        }

        public void DrawGameReady(SpriteBatch g)
        {
            g.Draw(textureFarmBackGround, new Rectangle(0, 0, textureFarmBackGround.Width, textureFarmBackGround.Height), new Rectangle(iXScreen, 150, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);
            if (isInBounds(Mouse.GetState().X, Mouse.GetState().Y, 270, 220, textureStartGame1.Width, textureStartGame1.Height))
            {
                g.Draw(textureStartGame0, new Rectangle(270, 220, textureStartGame0.Width, textureStartGame0.Height), Color.White);
            }
            else
            {
                g.Draw(textureStartGame1, new Rectangle(270, 220, textureStartGame1.Width, textureStartGame1.Height), Color.White);
            }
            giacam1.Draw(g);
            giacam2.Draw(g);
            giacam3.Draw(g);
            g.Draw(textureDay, new Rectangle(0, 170, 1024, 10), Color.White);
            g.Draw(texturePointer, new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, texturePointer.Width, texturePointer.Height), Color.White);
        }

        public void UpdateGameRunning()
        {
            if (_Ro.ILife == 0)
            {
                Game1.State = Game1.GameState.GameOver;
            }
            for (int i = 0; i < _lstGiaCam.Count; i++) // duyet danh sach gia cam
            {
                _lstGiaCam[i].DeTrung();
                _lstGiaCam[i].Move();
                for (int iegg = 0; iegg < _lstGiaCam[i].LstEgg.Count; iegg++) // duyet danh sach trung
                {
                    if (_lstGiaCam[i].LstEgg[iegg].IsMoving)
                    {
                        _lstGiaCam[i].LstEgg[iegg].Move();
                    }
                    if (_lstGiaCam[i].LstEgg[iegg].TOP + _lstGiaCam[i].LstEgg[iegg].HEIGHT >= _Ro.TOP + 35 && _lstGiaCam[i].LstEgg[iegg].TOP + _lstGiaCam[i].LstEgg[iegg].HEIGHT <= _Ro.TOP + 60)
                    {
                        if (_Ro.CheckCollisions(_lstGiaCam[i].LstEgg[iegg]))
                        {
                            if (_lstGiaCam[i].LstEgg[iegg].TYPE == Egg.Type.Trung)
                            {
                                _Ro.IScores++;
                                _iCountEgg++;
                                _iCountEgg2++;
                                _iCountEgg3++;
                                XuLyTangTocDoTrung();
                                XuLyTangDoCaoDayVaRo();
                                XuLyTangMang();
                            }
                            else if (_lstGiaCam[i].LstEgg[iegg].TYPE == Egg.Type.ChatThai)
                            {
                                _Ro.GiamMang();
                            }
                            _lstGiaCam[i].HuyTrung(iegg);
                        }
                    }
                    else if (_lstGiaCam[i].LstEgg.Count > 0)
                    {
                        if (_Rope.CheckCollisions(_lstGiaCam[i].LstEgg[iegg]))
                        {
                            if (_lstGiaCam[i].LstEgg[iegg].TYPE == Egg.Type.Trung)
                            {
                                _Ro.GiamMang();
                            }
                            _lstGiaCam[i].HuyTrung(iegg);
                        }
                    }
                }//end duyet danh sach trung

            } // end duyet danh sach gia cam
           

        }

        public void DrawGameRunning(SpriteBatch g)
        {
            g.Draw(textureFarmBackGround, new Rectangle(-220, -100, 1024, 768), Color.White);
            _Ro.VeHinhRoNho(g);
            for (int i = 0; i < _lstGiaCam.Count; i++)
            {
                _lstGiaCam[i].Draw(g);
                for (int iegg = 0; iegg < _lstGiaCam[i].LstEgg.Count; iegg++)
                {
                    _lstGiaCam[i].LstEgg[iegg].Draw(g);
                }
            }
            _Ro.Draw(g);
            _Rope.Draw(g);
            g.Draw(textureDay, new Rectangle(0, 170, 1024, 10), Color.White);
            g.DrawString(gameFont,"Scores: " + _Ro.IScores, new Vector2(Game1._iWidthWindow - 210, 10), Color.White);
           // g.DrawString(gameFont, "count: " + _iCountEgg2, new Vector2(400, 400), Color.Wheat);
            //g.DrawString(gameFont, "speed: " + iSpeedEgg, new Vector2(400, 350), Color.Teal);
            //g.DrawString(gameFont, "State Game: " + Game1.State, new Vector2(200, 350), Color.Aqua);
        }

        public void UserEvent(KeyboardState keyState)
        {
            if (Mouse.GetState().X + _Ro.WIDTH <= Game1._iWidthWindow && Mouse.GetState().X >= 0 &&
                          Mouse.GetState().Y <= Game1._iHeightWindow && Mouse.GetState().Y >= 0)
            {
                _Ro.LEFT = Mouse.GetState().X;
            }
           
            if (keyState.IsKeyDown(Keys.Right))
            {
                _Ro.MoveRigth();
            }
            if (keyState.IsKeyDown(Keys.Left))
            {
                _Ro.MoveLeft();
            }
            if (lastKeyState.IsKeyDown(Keys.G) && keyState.IsKeyUp(Keys.G))
            {
                ThemGiaCam(GiaCam.Type.Ga);
            }
            if (lastKeyState.IsKeyDown(Keys.V) && keyState.IsKeyUp(Keys.V))
            {
                ThemGiaCam(GiaCam.Type.Vit);
            }
            if (lastKeyState.IsKeyDown(Keys.N) && keyState.IsKeyUp(Keys.N))
            {
                ThemGiaCam(GiaCam.Type.Ngan);
            }
            if (lastKeyState.IsKeyDown(Keys.H) && keyState.IsKeyUp(Keys.H))
            {
                Game1.State = Game1.GameState.Pause;
                Game1.Screen = Game1.GameScreen.Tutorial;
                for (int i = 0; i < _lstGiaCam.Count; i++)
                {
                    _lstGiaCam[i].StopTimer();
                }
            }
            lastKeyState = keyState;
        }
        private void InitListGiaCam()
        {
            _lstGiaCam = new List<GiaCam>();
            GiaCam gc;
            for (int i = 0; i < 1; i++)
            {
                int iType = random.Next(3);
                switch (iType)
                {
                    case 0:
                        gc = new GiaCam(textureGa, 200 + i * 100, GiaCam.constTOP, textureGa.Width, textureGa.Height);
                        gc.GetType(GiaCam.Type.Ga);
                        _lstGiaCam.Add(gc);
                        break;
                    case 1:
                        gc = new GiaCam(textureGa, 200 + i * 100, GiaCam.constTOP, textureGa.Width, textureGa.Height);
                        gc.GetType(GiaCam.Type.Vit);
                        _lstGiaCam.Add(gc);
                        break;
                    case 2:
                        gc = new GiaCam(textureGa, 200 + i * 100, GiaCam.constTOP, textureGa.Width, textureGa.Height);
                        gc.GetType(GiaCam.Type.Ngan);
                        _lstGiaCam.Add(gc);
                        break;
                    default:
                        break;
                }
            }
        }
        private void InitGiaCamReady()
        {
            giacam1 = new GiaCam(textureGa, 200, GiaCam.constTOP, textureGa.Width, textureGa.Height);
            giacam1.GetType(GiaCam.Type.Ga);
            giacam2 = new GiaCam(textureGa, 300, GiaCam.constTOP, textureGa.Width, textureGa.Height);
            giacam2.GetType(GiaCam.Type.Vit);
            giacam3 = new GiaCam(textureGa, 400, GiaCam.constTOP, textureGa.Width, textureGa.Height);
            giacam3.GetType(GiaCam.Type.Ngan);
        }
        private void ThemGiaCam(GiaCam.Type type)
        {
            int left = random.Next(graphics.PreferredBackBufferWidth - 82);
            GiaCam gc = new GiaCam(textureGa, left, GiaCam.constTOP, textureGa.Width, textureGa.Height);
            gc.GetType(type);
            _lstGiaCam.Add(gc);
        }
        private void XuLyTangDoCaoDayVaRo()
        {
            if (_Rope.TOP >= 350)
            {
                if (_iCountEgg2 == 50)
                {
                    _Rope.TOP = _Rope.TOP - 20;
                    _Ro.TOP = _Ro.TOP - 20;
                    _iCountEgg2 = 0;
                }
            }
        }
        private void XuLyTangTocDoTrung()
        {
            if (iSpeedEgg < 13)
            {
                if (_iCountEgg == 20)
                {
                    iSpeedEgg++;
                    int iType = random.Next(3);
                    switch (iType)
                    {
                        case 0:
                            ThemGiaCam(GiaCam.Type.Ga);
                            break;
                        case 1:
                            ThemGiaCam(GiaCam.Type.Vit);
                            break;
                        case 2:
                            ThemGiaCam(GiaCam.Type.Ngan);
                            break;
                        default:
                            break;
                    }
                    _iCountEgg = 0;
                }
            }
        }
        private void XuLyTangMang()
        {
            if (_iCountEgg3 == 60)
            {
                _Ro.TangMang();
                _iCountEgg3 = 0;
            }
        }
        public static bool isInBounds(int x, int y, int left, int top, int width, int height)
        {
            if (x > left && x < left + width - 1 && y > top
                    && y < top + height - 1)
                return true;
            return false;
        }
        private void CameraMove()
        {
            if (CameraDirection == GiaCam.Direction.Right)
            {
                if (iXScreen + 630 < textureFarmBackGround.Width)
                {
                    iXScreen++;
                }
                else
                {
                    CameraDirection = GiaCam.Direction.Left;
                }
            }
            if (CameraDirection == GiaCam.Direction.Left)
            {
                if (iXScreen > 0)
                {
                    iXScreen--;
                }
                else
                {
                    CameraDirection = GiaCam.Direction.Right;
                }
            }
        }
        #endregion
    }
}
